$(function () {


    var recentPayDatagrid = $("#recentPayDatagrid");

    recentPayDatagrid.datagrid({
        url: 'Consunables/recentPay',
        fit:true,
        title:'最近支出的5条信息',
        rownumbers:true,
        /*pagination:true,*/
        singleSelect:true,
        toolbar:"#toolbar",
        fitColumns:true,
        columns: [[
            /*使用遍历:遍历的是Velocity中上下文的对象:
            * fieldList
            *                  {field: 'id', title: 'id', width: '10%',align:  'center'},
            * */
            {field: 'id', title: '编号',align:  'center'},
            {field: 'name', title: '名称', width: '20%',align:  'center'},
            {field: 'date', title: '时间', width: '25%',align:  'center'},
            {field: 'num', title: '数量', width: '25%',align:  'center'},
            {field: 'price', title: '价格', width: '25%',align:  'center'}
        ]]
    });

    $("#cc").combobox({
        onChange:function (v) {
            //每次下拉框改变清空div
            var total = 0;
            document.getElementById('divbutton').innerHTML='';
            $.get("Consunables/payAll",{"consunablesId":v},function (data) {
                /*追加*/
                $("#ff").form("load",data);
                var list = data.list;
                $.each(list,function (i,o) {
                    $("#divbutton").append("<button>"+o.name+"</button>")
                });
            })

        }
    });

    $("a").on("click", function () {
        var methods = $(this).data("method");
        if(methods){
            itsource[methods]();
        }
    });

    var itsource={
        "in":function () {
            $("#dd").dialog("open").dialog("center")
        },
        "submit":function () {
            $("#payfrom").form('submit',{
                url:'Consunables/addPay',/*Consunables*/
                onSubmit:function () {
                    /*return true;*/
                },
                //后台执行完毕回调
                success:function (data) {
                    //如果正常执行刷新页面 否则提示错误信息$.parseJSON(data);
                    var d = $.parseJSON(data);
                    if(d.success){
                        //报错成功
                        $("#dd").dialog("close");
                        $.messager.alert("保存成功")
                    }else {
                        alert(1)
                        $.messager.alert("操作失败","参数异常,请确认参数是否正确","error")
                    }
                }
            })
        },
        //点击每日支出弹出饼图
        'day':function (o) {
            pieFunction( '当日',1);
        },
        'week':function (o) {
            pieFunction( '本周',2);
        },
        'yue':function (o) {
            pieFunction( '本月',3);
        },
        'year':function (o) {
            pieFunction( '本年',4);
        }

    }

    function pieFunction(time,term) {
       /* console.debug("=="+term)*/
        $.get("Consunables/term",{"term":term},function (data) {
            Highcharts.chart('container', {
                chart: {
                    /*   plotBackgroundColor: null,
                       plotBorderWidth: null,
                       plotShadow: false,*/
                    type: 'pie'
                },
                title: {
                    text: "<span style='color: #00bbee'>"+time+"</span>"+'支出情况',
                },
                //去除水映
                credits: {
                    enabled: false
                },
                tooltip: {
                    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: true,
                            format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                            style: {
                                color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                            }
                        }
                    }
                },
                series: [{
                    name: '比列',
                    colorByPoint: true,
                    //后台查询的饼图数据展示data可以是数组或则json
                    data: data
                }]

            });
        })

    }

    onLoadSuccess:{
       /* alert(1)*/
        $(recentPayDatagrid).datagrid('fixRowHeight');
        itsource['yue']();
    }

})